//using Chromely.Core.Configuration;
using Chromely.Core.Network;
using MyChromelyApp1.Models;
using MyChromelyApp1.Utils;

namespace MyChromelyApp1.ChromelyControllers
{
    /// <summary>
    /// 笔记本控制器
    /// </summary>
    [ChromelyController(Name = "NotebookController")]
    public class NotebookController : ChromelyController
    {
        //private readonly IChromelyConfiguration _config;

        // /// <summary>
        // /// 构造函数
        // /// </summary>
        // /// <param name="config"></param>
        // public NotebookController(IChromelyConfiguration config)
        // {
        //    this._config = config;
        // }

        /// <summary>
        /// 获取全部笔记本
        /// </summary>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notebookcontroller/allnotebook")]
        public ResData GetAllNotebooks()
        {
            var notebooks = new List<string>();
            var dirInfo = new DirectoryInfo(MyConfig.localData);

            foreach (var dir in dirInfo.GetDirectories())
            {
                notebooks.Add(dir.Name);
            }
            return new ResData(200, "获取成功", notebooks);
        }

        /// <summary>
        /// 添加笔记本
        /// </summary>
        /// <param name="name">新笔记本名称</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notebookcontroller/addnotebook")]
        public ResData CreateNotebook(string name)
        {
            try
            {
                Directory.CreateDirectory(Path.Combine(MyConfig.localData, name));
                return new ResData(200, "创建成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "创建失败：" + e.Message);
            }
        }


        /// <summary>
        /// 获取当前笔记本下的全部笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notebookcontroller/notebook")]
        public ResData GetNoteOfCurrentNotebook(string currentNotebook)
        {
            var notes = new List<string>();

            var notebookDir = Path.Combine(MyConfig.localData, currentNotebook);
            try
            {
                var notebookDirInfo = new DirectoryInfo(notebookDir);

                foreach (var file in notebookDirInfo.GetFiles())
                {
                    notes.Add(file.Name.Replace(".md", ""));
                }
                return new ResData(200, "获取成功", notes);
            }
            catch (Exception e)
            {
                return new ResData(500, "获取失败" + e.Message);
            }

        }



        /// <summary>
        /// 删除笔记本
        /// </summary>
        /// <param name="currentNotebook"></param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notebookcontroller/removenotebook")]
        public ResData RemoveNotebook(string currentNotebook)
        {
            var path = Path.Combine(MyConfig.localData, currentNotebook);
            try
            {
                // 删除文件夹
                Directory.Delete(path, true);
                return new ResData(200, "删除成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "删除失败：" + e.Message);
            }
        }
    }
}
